// 一天的时间戳
let time = 86400;
let weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");

// 今天的数据
export const getDate = (nowDay) => {
    let nowDate = "";
    if (nowDay) {
        nowDate = nowDay;
    } else {
        nowDate = new Date();
    }
    let date = {
        year: nowDate.getFullYear(),
        month: (nowDate.getMonth() + 1) > 9 ? (nowDate.getMonth() + 1) : '0' + (nowDate.getMonth() + 1),
        date: nowDate.getDate() > 9 ? nowDate.getDate() : '0' + nowDate.getDate(),
        week: weeks[nowDate.getDay()]
    };
    return date;
}

/**
 * 二个日期零点的时间相减(2020-10-10 - 2020-10-09)
 * val：过去某个时间
 */
export const nowtime = (val1, val2) => {
    let nowtime = parseInt(new Date(val1).getTime() / 1000);
    let oldTime = parseInt(new Date(val2).getTime() / 1000);
    return parseInt((nowtime - oldTime) / time);
}

/**
 * val = '2020-11-01 00:00:00'返回2020-11-01
 * val = ""返回当前时间(2020-10-01)
 */
export const getYMD = (val) => {
    let d = "";
    if (val) {
        d = new Date(new Date(val).getTime());
    } else {
        d = new Date();
    }
    return d.getFullYear() + "-" + (d.getMonth() + 1 > 9 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1)) + "-" + (d.getDate() > 9 ? d.getDate() : "0" + d.getDate())
}

/**
 * 返回2020-11-01
 * n=0,默认是今天
 */
export const getOldYMD = (n = 0) => {
    let oldTime = parseInt(new Date().getTime() / 1000) - time * n;
    let d = new Date(oldTime * 1000);
    return d.getFullYear() + "-" + (d.getMonth() + 1 > 9 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1)) + "-" + (d.getDate() > 9 ? d.getDate() : "0" + d.getDate())
}


// 2020-10-01 00:00:00返回00:00:00
export const getHMS = (val) => {
    let d = new Date(new Date(val).getTime());
    return (d.getHours() > 9 ? d.getHours() : "0" + d.getHours()) + ":" + (d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes()) + ":" + (d.getSeconds() > 9 ? d.getSeconds() : "0" + d.getSeconds());
}


// 2020-10-01 00:00:00返回 "星期一"
export const getWeeks = (val) => {
    let d = new Date(new Date(val).getTime());
    return weeks[d.getDay()];
}
